package com.hankol.newretail.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.baselibrary.utils.ScreenUtils;
import com.hankol.newretail.R;

import per.wsj.library.AndRatingBar;

/**
 * @author:hanke 2022/9/13
 * desc:
 */
public class EvaluationRatingBar extends LinearLayout {
    private AndRatingBar andRatingBar;
    private TextView ratingLevel;
    private ChangeRantingListener changeRantingListener;

    public void setChangeRantingListener(ChangeRantingListener changeRantingListener) {
        this.changeRantingListener = changeRantingListener;
    }

    public EvaluationRatingBar(Context context) {
        super(context);
        initView();
    }

    public EvaluationRatingBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public EvaluationRatingBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public EvaluationRatingBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView() {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.view_evaluation_ratingbar, this);
        andRatingBar = inflate.findViewById(R.id.arb_evaluation);
        ratingLevel = inflate.findViewById(R.id.tv_evaluate_level);
        andRatingBar.setIsIndicator(false);
        andRatingBar.setOnRatingChangeListener(new AndRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChanged(AndRatingBar ratingBar, float rating, boolean fromUser) {
                if (changeRantingListener != null) {
                    changeRantingListener.onChange((int) rating);
                }
                switch ((int) rating) {
                    case 1:
                        ratingLevel.setText("非常差");
                        break;
                    case 2:
                        ratingLevel.setText("很差");
                        break;
                    case 3:
                        ratingLevel.setText("一般");
                        break;
                    case 4:
                        ratingLevel.setText("很好");
                        break;
                    case 5:
                        ratingLevel.setText("非常好");
                        break;
                }
            }
        });
    }

    public int getRatingLevel() {
        return (int) andRatingBar.getRating();
    }

    public void setRatingLevel(int level,boolean showText) {
        andRatingBar.setRating(level);
        switch (level) {
            case 1:
                ratingLevel.setText("非常差");
                break;
            case 2:
                ratingLevel.setText("很差");
                break;
            case 3:
                ratingLevel.setText("一般");
                break;
            case 4:
                ratingLevel.setText("很好");
                break;
            case 5:
                ratingLevel.setText("非常好");
                break;
            default:
                ratingLevel.setText("");
                break;
        }
        if (!showText){
            ratingLevel.setText("");
        }
    }

    public void setStarStyle(int starSize, boolean isIndicator, float space, int padding, int levelTextSize) {
        ViewGroup.LayoutParams lp = andRatingBar.getLayoutParams();
        lp.height = ScreenUtils.dpToPx(starSize);
        andRatingBar.setLayoutParams(lp);
        andRatingBar.setIsIndicator(isIndicator);
        andRatingBar.setStarSpacing(space);
        ratingLevel.setPadding(ScreenUtils.dpToPx(padding), 0, 0, 0);
        ratingLevel.setTextSize(TypedValue.COMPLEX_UNIT_DIP, levelTextSize);
    }

    public interface ChangeRantingListener {
        void onChange(int rate);
    }
}
